#include "../head/server.h"
#include "../head/md5.h"

void *threadFunc(void *arg)
{
	threadPool_t *pthreadPool = (threadPool_t *)arg;
	pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
	// 从配置文件中读取出连接数据库所需的数据
	char mysql_user[100] = {0};
	getparameter("mysql_user", mysql_user); // getparameter.c
	// printf("mysql_user: %s\n", mysql_user);
	char mysql_password[100] = {0};
	getparameter("mysql_password", mysql_password); // getparameter.c
	// printf("mysql_password: %s\n", mysql_password);
	char mysql_database[100] = {0};
	getparameter("mysql_database", mysql_database); // getparameter.c
	// printf("mysql_database: %s\n", mysql_database);

	MYSQL *mysql = mysql_init(NULL);
	MYSQL *ret = mysql_real_connect(
		mysql,
		"localhost",
		mysql_user,
		mysql_password,
		mysql_database,
		0, NULL, 0);
	if (ret == NULL)
	{
		fprintf(stderr, "mysql_real_connect:%s\n", mysql_error(mysql));
	}
	pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);

	while (1)
	{
		pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
		while (pthreadPool->taskQueue.size <= 0)
		{
			pthread_cond_wait(&pthreadPool->taskQueue.cond, &pthreadPool->taskQueue.mutex);
		}
		int netfd = pthreadPool->taskQueue.front->net_fd;
		deQueue(&pthreadPool->taskQueue);
		pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);

		handle(netfd, mysql); // 子线程执行任务
	}
	mysql_close(mysql);
	return NULL;
}